package com.mobileservice.autoreply.ui.customizeui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.mobileservice.autoreply.R;
import com.mobileservice.autoreply.activity.AddContactActivity;
import com.mobileservice.autoreply.activity.Add_Reply_ContactActivity;
import com.mobileservice.autoreply.activity.AutoReplyActivity;
import com.mobileservice.autoreply.activity.BlockMessageActivity;
import com.mobileservice.autoreply.activity.BlockinComingCallActivity;
import com.mobileservice.autoreply.activity.MainActivity;
import com.mobileservice.autoreply.activity.SettingActivity;
import com.mobileservice.autoreply.common.constant.CommConstant;

public class TopBar extends RelativeLayout implements OnClickListener {

	public ImageButton backButton, confirm_btn, addButton;
	private Context mContext;

	public TopBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		LayoutInflater.from(context).inflate(R.layout.topbar_layout, this, true);
		mContext = context;
		/** Initial View */
		Initial();
	}

	private void Initial() {
		backButton = (ImageButton) findViewById(R.id.backbutton);
		confirm_btn = (ImageButton) findViewById(R.id.list_setting_gear);
		addButton = (ImageButton) findViewById(R.id.add_topbar_btn);

		if (!(mContext instanceof MainActivity)) {
			backButton.setVisibility(View.VISIBLE);
			confirm_btn.setVisibility(!(mContext instanceof SettingActivity) ? View.GONE : View.VISIBLE);
			addButton.setVisibility(!(mContext instanceof SettingActivity) ? View.VISIBLE : View.GONE);
			confirm_btn.setOnClickListener(this);
			backButton.setOnClickListener(this);
			addButton.setOnClickListener(this);
		}

	}

	public void shiftButton(boolean ApdaterCall) {
		confirm_btn.setVisibility(ApdaterCall ? View.VISIBLE : View.GONE);
		addButton.setVisibility(ApdaterCall ? View.GONE : View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch (id) {
		case R.id.backbutton:
			((Activity) mContext).onBackPressed();
			break;
		case R.id.list_setting_gear:
			if (mContext instanceof BlockinComingCallActivity) {
				((BlockinComingCallActivity) mContext).Save();
			} else if (mContext instanceof BlockMessageActivity) {
				((BlockMessageActivity) mContext).Save();
			} else if (mContext instanceof AutoReplyActivity) {
				((AutoReplyActivity) mContext).Save();
			} else if (mContext instanceof SettingActivity) {
				((SettingActivity) mContext).Save();
			}
			break;
		case R.id.add_topbar_btn:
			if (mContext instanceof AddContactActivity) {

			} else if (mContext instanceof BlockinComingCallActivity) {

				Intent intent = new Intent(mContext, AddContactActivity.class);
				intent.putExtra(CommConstant.CALL_FROM_WHICH_ACTIVITY, CommConstant.BLOCK_CALL);
				((BlockinComingCallActivity) mContext).startActivityForResult(intent, CommConstant.ADD_CONTACT);
			} else if (mContext instanceof BlockMessageActivity) {

				Intent intent = new Intent(mContext, AddContactActivity.class);
				intent.putExtra(CommConstant.CALL_FROM_WHICH_ACTIVITY, CommConstant.BLOCK_SMS);
				((BlockMessageActivity) mContext).startActivityForResult(intent, CommConstant.ADD_CONTACT);
			} else if (mContext instanceof AutoReplyActivity) {

				Intent intent = new Intent(mContext, Add_Reply_ContactActivity.class);
				((AutoReplyActivity) mContext).startActivity(intent);
			}
			break;
		}
	}

}
